<?php

namespace app\admin\model\chase;

use think\Model;
use traits\model\SoftDelete;

class Teacher extends Model
{

    use SoftDelete;

    

    // 表名
    protected $table = 'teachers';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'is_freeze_text'
    ];
    

    
    public function getIsFreezeList()
    {
        return ['0' => __('Is_freeze 0'), '1' => __('Is_freeze 1')];
    }


    public function getIsFreezeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_freeze']) ? $data['is_freeze'] : '');
        $list = $this->getIsFreezeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function classes()
    {
        return $this->belongsToMany(Classes::class, 'teacher_classes', 'class_id', 'teacher_id');
    }

    public static function getStudents($phone, $isSuperAdmin = false)
    {
        $class_id = self::getClassess($phone, $isSuperAdmin);
        $students = Student::getStudentsByClass($class_id);
        return $students;
    }

    public static function getClassess($phone, $isSuperAdmin)
    {
        $teachers = self::with('classes')->where('phone', $phone)->find();
        if ($isSuperAdmin) {
            $class_id = Classes::column('id');
        } else {
            if ($teachers) {
                $class_id = array_column($teachers->classes, 'id');
            } else {
                $class_id = [];
            }
        }
        return $class_id;
    }

}
